package org.jeecg.modules.hxrencai.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.hxrencai.bo.AreaBo;
import org.jeecg.modules.hxrencai.dto.UniversityDto;
import org.jeecg.modules.hxrencai.entity.University;
import org.jeecg.modules.hxrencai.service.IUniversityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 高校信息
 * @Author: jeecg-boot
 * @Date: 2019-11-24
 * @Version: V1.0
 */
@Slf4j
//@Api(tags = "高校信息")
//@RestController
//@RequestMapping("/hxrencai/university")
public class UniversityController extends JeecgController<University, IUniversityService> {
    @Autowired
    private IUniversityService universityService;

    /**
     * 分页列表查询
     *
     * @param req
     * @return
     */
//    @AutoLog(value = "高校信息-分页列表查询")
//    @ApiOperation(value = "高校信息-分页列表查询 1 福建高职 2 福建本科   5 全国本科  6 福建中职", notes = "高校信息-分页列表查询")
//    @GetMapping(value = "/list")
    public Result<?> queryPageList(UniversityDto dto,
                                   HttpServletRequest req) {
        University university = new University();
        BeanUtil.copyProperties(dto, university);
        QueryWrapper<University> queryWrapper = QueryGenerator.initQueryWrapper(university, req.getParameterMap());
        int pageNo = dto.getPageNo() == null ? 1 : dto.getPageNo();
        int pageSize = dto.getPageSize() == null ? 10 : dto.getPageSize();
        Page<University> page = new Page<University>(pageNo, pageSize);
        if (StringUtils.isBlank(university.getImportType())) {
            queryWrapper.eq("import_type", "1");
        }
        IPage<University> pageList = universityService.page(page, queryWrapper);
        return Result.ok(pageList);
    }


    //    @AutoLog(value = "福建高职-获取所有")
//    @ApiOperation(value = "福建高职-获取所有", notes = "福建高职-获取所有")
//    @GetMapping(value = "/getFjgzAll")
    public List<University> getFjgzAll() {
        LambdaQueryWrapper<University> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(University::getImportType, "1");
        List<University> list = universityService.list(queryWrapper);
        return list;
    }

    //    @AutoLog(value = "福建本科-获取所有")
//    @ApiOperation(value = "福建本科-获取所有", notes = "福建本科-获取所有")
//    @GetMapping(value = "/getFjbkAll")
    public List<University> getFjbkAll() {
        LambdaQueryWrapper<University> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(University::getImportType, "2");
        List<University> list = universityService.list(queryWrapper);
        return list;
    }

    //    @AutoLog(value = "全国本科-获取所有")
//    @ApiOperation(value = "全国本科-获取所有", notes = "全国本科-获取所有")
//    @GetMapping(value = "/getQgbkAll")
    public List<University> getQgbkAll() {
        LambdaQueryWrapper<University> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(University::getImportType, "5");
        List<University> list = universityService.list(queryWrapper);
        return list;
    }

    //    @AutoLog(value = "福建中职-获取所有")
//    @ApiOperation(value = "福建中职-获取所有", notes = "福建中职-获取所有")
//    @GetMapping(value = "/getQgzzAll")
    public List<University> getQgzzAll() {
        LambdaQueryWrapper<University> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(University::getImportType, "6");
        List<University> list = universityService.list(queryWrapper);
        return list;
    }

    //    @AutoLog(value = "学校类型省份数量统计")
//    @ApiOperation(value = "学校类型省份数量统计 导入类型 1 福建高职 2 福建本科   5 全国本科  6 福建中职", notes = "学校类型省份数量统计")
//    @GetMapping(value = "/queryProvinceNumByArea/{importType}")
    public List<AreaBo> queryProvinceNumByArea(@ApiParam(required = true, value = "导入类型 1 福建高职 2 福建本科   5 全国本科  6 福建中职") @PathVariable String importType) {
        return universityService.queryProvinceNumGroupByArea(importType);
    }

    /**
     * 添加
     *
     * @param university
     * @return
     */
//    @AutoLog(value = "高校信息-添加")
    //@ApiOperation(value = "高校信息-添加", notes = "高校信息-添加")
//    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody University university) {
        universityService.save(university);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param university
     * @return
     */
//    @AutoLog(value = "高校信息-编辑")
    //@ApiOperation(value = "高校信息-编辑", notes = "高校信息-编辑")
//    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody University university) {
        universityService.updateById(university);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
//    @AutoLog(value = "高校信息-通过id删除")
    // @ApiOperation(value = "高校信息-通过id删除", notes = "高校信息-通过id删除")
//    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        universityService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
//    @AutoLog(value = "高校信息-批量删除")
    // @ApiOperation(value = "高校信息-批量删除", notes = "高校信息-批量删除")
//    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.universityService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
//    @AutoLog(value = "高校信息-通过id查询")
//    @ApiOperation(value = "高校信息-通过id查询", notes = "高校信息-通过id查询")
//    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        University university = universityService.getById(id);
        return Result.ok(university);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param university
     */
//    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, University university) {
        return super.exportXls(request, university, University.class, "高校信息");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, University.class);
    }

}
